//
//  PicCollectionViewCell.m
//  悦享
//
//  Created by dllo on 16/3/17.
//  Copyright © 2016年 张洪国. All rights reserved.
//

#import "PicCollectionViewCell.h"
#import <UIImageView+WebCache.h>

@interface PicCollectionViewCell ()

@property (nonatomic, assign) BOOL isSelect;/**<  判断是否收藏 */

@end

@implementation PicCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.picImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_picImageView];
        
        
        self.backImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_backImageView];
        
        
        self.titleLabel1 = [[UILabel alloc] init];
        [self.contentView addSubview:_titleLabel1];
        
        
        self.logaImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_logaImageView];
        
        
        self.descLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_descLabel];
        
        
        self.likeImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_likeImageView];
        
        self.likeLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_likeLabel];
        
        _collecImageView = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_collecImageView];
        
        
        _collectLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_collectLabel];
        
        _isSelect = YES;
    }
    return self;
}

- (void)setModel:(TVModel *)model{
    _model = model;
    
    [_picImageView sd_setImageWithURL:[NSURL URLWithString:_model.picUrl] placeholderImage:[UIImage imageNamed:PLACE_HLODER]];
    
    _titleLabel1.text = _model.name;
    
    [_logaImageView sd_setImageWithURL:[NSURL URLWithString:_model.icon] placeholderImage:[UIImage imageNamed:PLACE_HLODER]];
    
    _backImageView.image = [UIImage imageNamed:@"trip_edit_title_shadow@2x.png"];
    
    _likeImageView.image = [UIImage imageNamed:@"iconfont-zan.png"];
    
    _descLabel.text = _model.desc;
    
    _likeLabel.text = [_model.admireCount stringValue];
    
    [_collecImageView setImage:[UIImage imageNamed:@"iconfont-iconfontshoucang.png"] forState:UIControlStateNormal];
    
    _collectLabel.text = @"收藏";
    
}


- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    /**<  底层图片 */
    _picImageView.layer.cornerRadius = 10;
    _picImageView.layer.masksToBounds = YES;
    _picImageView.frame = CGRectMake(50, 0, layoutAttributes.size.width - 100, layoutAttributes.size.height);
    /**<  字体下面的渐变将图片 */
    _backImageView.frame = CGRectMake(50, layoutAttributes.size.height - 100, layoutAttributes.size.width - 100, 100);
    
    _backImageView.layer.cornerRadius = 10;
    _backImageView.layer.masksToBounds = YES;
    /**<  类别图片 */
    _logaImageView.frame = CGRectMake(50,30, 70, 20);
    
    /**<  标题 */
    _titleLabel1.frame = CGRectMake(60, layoutAttributes.size.height - 50, 200, 35);
    _titleLabel1.textColor = [UIColor whiteColor];
    _titleLabel1.font = [UIFont boldSystemFontOfSize:15];
    
    _descLabel.frame = CGRectMake(60, layoutAttributes.size.height - 25, 240, 20);
    _descLabel.font = [UIFont systemFontOfSize:12];
    _descLabel.textColor = [UIColor whiteColor];
    
    
    _likeImageView.frame = CGRectMake(layoutAttributes.size.width - 115, layoutAttributes.size.height - 45, 20, 20);
    
    _likeLabel.frame = CGRectMake(layoutAttributes.size.width - 122, layoutAttributes.size.height - 20, 40, 10);
    _likeLabel.font = [UIFont systemFontOfSize:10];
    _likeLabel.textColor = [UIColor whiteColor];
    
    _collecImageView.frame = CGRectMake(layoutAttributes.size.width - 82, layoutAttributes.size.height - 43, 18, 18);
    [_collecImageView addTarget:self action:@selector(colectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    _collectLabel.frame = CGRectMake(layoutAttributes.size.width - 82, layoutAttributes.size.height - 20, 30, 10);
    _collectLabel.font = [UIFont systemFontOfSize:10];
    _collectLabel.textColor = [UIColor whiteColor];
    
}
//收藏button的点击方法
- (void)colectButtonAction:(UIButton *)button{
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    
    keyAnimation.values = @[@(1.0), @(0.1), @(1.0), @(1.5), @(1.0)];
    keyAnimation.duration = 0.5;
    [button.layer addAnimation:keyAnimation forKey:@"keyAnomation"];
    
    if (_isSelect == NO) {
        [_collecImageView setImage:[UIImage imageNamed:@"iconfont-iconfontshoucang.png"] forState:UIControlStateNormal];
    }else{
        [_collecImageView setImage:[UIImage imageNamed:@"iconfont-iconfontshoucang-2.png"] forState:UIControlStateNormal];
    }
   
    _isSelect = !_isSelect;
    
}

@end
